﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HustQQInterface;
using HustQQClient.MsgDispatcher;

namespace HustQQClient
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
            
            InterfaceManager.RegisterNetInterface(new NetImplement());
            InterfaceManager.NetManager.RecieveTextFromSingle += new SingleTextMsg(NetManager_RecieveTextFromSingle);
            InterfaceManager.NetManager.ConfirmRecieveFile += new FileMsg(NetManager_ConfirmRecieveFile);
            InterfaceManager.NetManager.ConfirmSendFile += new BooleanMsg(NetManager_ConfirmSendFile);
            InterfaceManager.NetManager.SendFileOver += new Action(NetManager_SendFileOver);
            InterfaceManager.NetManager.ReceiveFileOver += new FileMsg(NetManager_ReceiveFileOver);
            
        }

        void Port_SendFileRate(double rate)
        {
            
        }

        void Port_RecieveFileRate(double rate)
        {
            
        }

        void NetManager_ReceiveFileOver(string userId, HustQQModel.FileInformation fileInfo, DateTime time)
        {
            MessageBox.Show("接受文件"+fileInfo.FileName+"完毕~");
        }

        void NetManager_SendFileOver()
        {
            MessageBox.Show("发送文件完毕~");
        }

        void NetManager_ConfirmSendFile(bool isOk)
        {
            if (isOk)
            {
                MessageBox.Show("对方同意接受您的文件");
            }
            else
            {
                MessageBox.Show("对方拒绝接受您的文件");
            }
        }

        void NetManager_ConfirmRecieveFile(string userId, HustQQModel.FileInformation fileInfo, DateTime time)
        {
            if (MessageBox.Show("您是否希望接受来自" + userId + "的文件?" + "文件名:" + fileInfo.FileName + "文件大小:" + fileInfo.FileSize.ToString() + "B", "File", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                this.Invoke(new Action(() =>
                {
                    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                    {
                        fileInfo.FilePath = folderBrowserDialog1.SelectedPath + "//" + fileInfo.FileName;
                    }
                }));
                InterfaceManager.NetManager.ReplyConfirm(true);
                return;
            }
            InterfaceManager.NetManager.ReplyConfirm(false);
        }

        void NetManager_RecieveTextFromSingle(string userId, string text, DateTime time)
        {
            MessageBox.Show("来自" + userId + "的消息,内容为:" + text + " 时间为:" + time.ToString());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(InterfaceManager.NetManager.Login(textBox1.Text, "34").ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            InterfaceManager.NetManager.SendTextToTeam(textBox1.Text, textBox5.Text, textBox4.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            InterfaceManager.NetManager.SendTextToSingle(textBox1.Text, textBox3.Text, textBox2.Text);
        }

        string filePath = "";

        private void button4_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                filePath = openFileDialog1.FileName;
                InterfaceManager.NetManager.SendFile("123", "234", filePath);
            }
        }
    }
}
